//
//  MSTypeAViewCell.m
//  MSJD
//
//  Created by xiangbo on 2018/9/22.
//  Copyright © 2018 xiangbo. All rights reserved.
//

#import "MSTypeAView.h"
#import "MSTitleView.h"
#import "MSTemplateCellUtil.h"
#import "MSDetailViewController.h"
#import "MSCollectionBaseCell.h"

@interface MSTypeAView ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,MSDetailViewDelegate>

@property (nonatomic, strong)MSTitleView *titleView;
@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, strong)NSString *showType;
@property (nonatomic, assign)CGSize itemSize;

@end

@implementation MSTypeAView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupSubviews];
    }
    return self;
}


- (void)setupSubviews{
    [self addSubview:self.titleView];
    [self addSubview:self.collectionView];
    
    [self.titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self);
        make.height.mas_equalTo(79.f);
    }];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleView.mas_bottom);
        make.left.right.bottom.equalTo(self);
    }];
}

#pragma mark -UIScrollViewDelegate
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if (!decelerate) {
        [self scrollEndAction:scrollView];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [self scrollEndAction:scrollView];
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    [self scrollEndAction:scrollView];
}

- (void)scrollEndAction:(UIScrollView*)scrollView {
    ((MSFoodData *)self.templateData).localOffset = scrollView.contentOffset;
}
#pragma mark dataSet
- (void)setupTemplateData:(MSFoodData *)templateData{
    [super setupTemplateData:templateData];
    [self.titleView refreshTitle:templateData];
    self.showType = templateData.showType;
    self.itemSize = [MSTemplateCellUtil getTemplateCellSize:self.showType];
    [self.collectionView reloadData];
    if (!CGPointEqualToPoint(self.collectionView.contentOffset, templateData.localOffset)) {
        self.collectionView.contentOffset = templateData.localOffset;
    }
    
}

#pragma mark UITableViewDelegate and DataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return ((MSFoodData *)self.templateData).contents.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    NSString *identifier = MSRedirectIdentifierDic[self.showType];
    if (!identifier) {
        identifier = @"MSCollectionBaseCell";
    }
    MSFoodContentModel *detailData = ((MSFoodData *)self.templateData).contents[indexPath.row];
    MSCollectionBaseCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    cell.modelData = detailData;
    return cell;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(11.f, 20.f, 11.f, 11);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return self.itemSize;
}

#pragma mark - UICollectionViewDataSource

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    MSFoodContentModel *detailData = ((MSFoodData *)self.templateData).contents[indexPath.row];
    [MSJumpConn jumpControllerWithObject:detailData];
//    [ presentViewController:detailVC animated:YES completion:nil];
}

#pragma mark -Property
- (MSTitleView *)titleView {
    if (!_titleView) {
        _titleView = [[MSTitleView alloc] init];
    }
    return _titleView;
}

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumLineSpacing = 11;
        layout.minimumInteritemSpacing = 11;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.backgroundColor = [UIColor whiteColor];
        
        [_collectionView registerClass:[MSCollectionBaseCell class] forCellWithReuseIdentifier:NSStringFromClass([MSCollectionBaseCell class])];
        [_collectionView registerClass:[MSTypeACollectionCell class] forCellWithReuseIdentifier:NSStringFromClass([MSTypeACollectionCell class])];
        [_collectionView registerClass:[MSVimgCollectionCell class] forCellWithReuseIdentifier:NSStringFromClass([MSVimgCollectionCell class])];
        
    }
    return _collectionView;
}

@end
